package com.sdy.kzyy.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.StringUtil;
import com.sdy.kzyy.biz.constants.Constants;
import com.sdy.kzyy.biz.model.Area;
import com.sdy.kzyy.biz.service.AreaService;
import com.sdy.kzyy.biz.service.OrderRecordService;
import com.sdy.kzyy.biz.service.StoreService;
import com.sdy.kzyy.biz.dto.StoreDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: 王越洋
 * @version: v1.0
 * @description: com.sdy.kzyy.web.controller
 * @date:2020/3/4
 */

@Slf4j
@Controller
@RequestMapping("/getStoreDisplay")
@Api(tags = "门店展示Api")
public class StoreDisplayController {
    @Autowired
    private StoreService storeService;
    @Autowired
    private OrderRecordService orderRecordService;
    @Autowired
    private AreaService areaService;

    @GetMapping("/getStorePage")
    @ResponseBody
    @ApiOperation(value = "门店展示搜索网点/地区模糊查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "店名/地区"),
            @ApiImplicitParam(name = "page", value = "分页数据"),
    })
    public Response getStorePage(String name, Page<StoreDTO> page) {
        if(StringUtil.isNotBlank(name)) {
            try {
                name = URLDecoder.decode(name, "UTF-8");
            }catch (Exception e) {
                return Response.error("");
            }
        }
        //查询当前区域的药店
        List<StoreDTO> areaPage = storeService.getNamePage(name, page);
        Integer areaPageSize = storeService.getNamePageSize(name);
        List<StoreDTO> transform = transform(areaPage);
        IPage<StoreDTO> storeVoPage = new Page<StoreDTO>(page.getCurrent(), page.getSize(), areaPageSize).setRecords(transform);
        return Response.success(storeVoPage);
    }


    @GetMapping("/getAreaPage")
    @ResponseBody
    @ApiOperation(value = "门店展示药店区域查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "药店区编号"),
            @ApiImplicitParam(name = "page", value = "分页数据"),
    })
    public Response getAreaPage(String areaCode, Page<StoreDTO> page) throws BizException {
        Assert.isNull(areaCode, "药店区域不能为空");

        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String str = sdf.format(d);
        //查询当前区域的药店
        List<StoreDTO> areaPage = storeService.getAreaPage(areaCode, page);
        Integer areaPageSize = storeService.getAreaPageSize(areaCode);
        List<StoreDTO> transform = transform(areaPage);
        IPage<StoreDTO> storeVoPage = new Page<StoreDTO>(page.getCurrent(), page.getSize(), areaPageSize).setRecords(transform);
        return Response.success(storeVoPage);
    }

    @GetMapping("/getAllArea")
    @ResponseBody
    @ApiOperation(value = "查询所有区县接口")
    public Response getAllArea() {
        List<Area> allArea = areaService.getAllArea();
        return Response.success(allArea);
    }


    /**
     * 重新生成列表
     * @param areaPage
     * @return
     */
    private List<StoreDTO> transform(List<StoreDTO> areaPage){
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String str = sdf.format(d);
        List<StoreDTO> storeVos = new ArrayList<>();
        for (StoreDTO store : areaPage) {
            StoreDTO storeVo = new StoreDTO();
            storeVo.setId(store.getId());
            storeVo.setName(store.getName());
            storeVo.setAreaCode(store.getAreaCode());
            storeVo.setAddress(Constants.REGION_CITY_NAME +store.getAreaName()+store.getAddress());
            storeVo.setAreaName(store.getAreaName());
            storeVo.setOnceCount(store.getOnceCount());
            storeVo.setStoreCode(store.getStoreCode());
            storeVo.setKzCount(store.getKzCount());
            storeVo.setUpdateTime(store.getUpdateTime());
            storeVos.add(storeVo);
        }
        return storeVos;
    }
}
